package com.aixin.project.db;

import com.aixin.project.data.Course;
import com.aixin.project.data.CourseOrder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA. User: Sufeng Date: 12-4-14 Time: 下午6:17 To change
 * this template use File | Settings | File Templates.
 */
public class CourseDao extends Dao {

    public static long insert(Course course) {
        return insert(
                "insert into course(title,description,expertid,starttime,endtime) values(?,?,?,?,?)",
                course.getTitle(), course.getDescription(), course
                .getExpertid(), course.getStarttime(), course
                .getEndtime());
    }

    public static Course find(long cid) {
        return (Course) find("select * from course where id=?", Course.class,
                cid);
    }

    public static Object findPoint(long cid) {
        return new Dao().findBy(
                "select sum(point) from courseorder  where courseid=?", null,
                new Object[]{cid});
    }

    public static Object findUsersByCid(long cid) {
        return new Dao().findBy(
                "select count(userid) from courseorder  where courseid=?",
                null, new Object[]{cid});
    }

    public static List<Course> commingCourseByEid(long expertid) {
        return list(
                "select * from course where expertid=? and  now()< starttime order by starttime asc limit 0,2",
                Course.class, expertid);
    }

    public static List<Integer> commingCourseIdByEid(long expertid) {
        List<Integer> list = null;
        List<Map<String, Object>> mapList = list("select id from course where expertid=? and  now()< starttime order by starttime asc limit 0,2",expertid);
        if(mapList!=null && mapList.size()>0){
            list = new ArrayList<Integer>();
            for(Map map:mapList){
                list.add((Integer) map.get("id"));
            }
        }
        return list;
    }

    public static List<Course> listCourseByExpert(long eid) {
        System.out.println("start listByExpert eid:" + eid);
        return list(
                "select * from course where expertid=? order by createtime",
                Course.class, eid);
    }

    public static List<Integer> listCourseIdByExpert(long eid) {
        List<Integer> list = null;
        List<Map<String, Object>> mapList = list("select id from course where expertid=? order by createtime",eid);
        if(mapList!=null && mapList.size()>0){
            list = new ArrayList<Integer>();
            for(Map map:mapList){
                list.add((Integer) map.get("id"));
            }
        }
        return list;
    }

    public static List<CourseOrder> listByUser(long uid) {
        return list(
                "select * from courseorder where userid=? order by createtime",
                CourseOrder.class, uid);
    }

    public static List<Course> listCourse() {
        return list("select * from course order by starttime desc",
                Course.class);
    }

    public static List<Course> listComingCourse() {
        return list(
                "select * from course order by starttime asc",
                Course.class);
    }

    public static List<Integer> listComingCourseId() {
        List<Integer> list = null;
        List<Map<String, Object>> mapList = list("select id from course where now()< starttime order by starttime asc");
        if(mapList!=null && mapList.size()>0){
            list = new ArrayList<Integer>();
            for(Map map:mapList){
                list.add((Integer) map.get("id"));
            }
        }
        return list;
    }

    public static int create(Course c) {
        return (int) insert(
                "insert into course(title,description,expertid,starttime,endtime,pointprice,baseprice,addprice,service,coursetype,genseeid,organizerurl,weburl,clienturl,zzzpass,jbpass,cyzpass) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                c.getTitle(), c.getDescription(), c.getExpertid(), c
                .getStarttime(), c.getEndtime(), c.getPointprice(), c
                .getBaseprice(), c.getAddprice(), c.getService(), c
                .getCoursetype(), c.getGenseeid(), c.getOrganizerurl(), c.getWeburl(), c.getClienturl(), c.getZzzpass(), c.getJbpass(), c.getCyzpass());
    }

    public static int update(Course c) {
        return update("update course set title=?,description=?,expertid=?,starttime=?,endtime=?,pointprice=?,baseprice=?,addprice=?,service=?,coursetype=?,zzzpass=?,jbpass=?,cyzpass=? where id=?",
                c.getTitle(), c.getDescription(), c.getExpertid(), c
                .getStarttime(), c.getEndtime(), c.getPointprice(), c
                .getBaseprice(), c.getAddprice(), c.getService(), c
                .getCoursetype(),c.getZzzpass(),c.getJbpass(),c.getCyzpass(), c.getId());
    }


    public static int delete(long cid) {
        return update("delete from course where id=? ", cid);
    }
    
    public static Object totalRs(int stats) {
    	String sql="";
		if(stats==1){
			sql="where starttime>now()";
		}else if(stats==2)
			sql="where starttime<now()";
		return findBy("select count(*) from course "+sql, 1, null);
	}
	
	public static List<Course> listBypagein(int num, int page,int stats) {
		String sql="";
		if(stats==1){
			sql="where starttime>now()";
		}else if(stats==2)
			sql="where starttime<now()";
		int param=(page - 1) * num;
		return list("select * from course "+sql+" order by createtime desc limit ?,?", Course.class,param,num);
	}
}
